/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tr.com.argela.tap.mot.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import tr.com.argela.tap.mot.logger.LoggerFactory;
import tr.com.argela.tap.mot.models.Role;
import tr.com.argela.tap.mot.models.User;

/**
 * @author arge
 */
public class AuthenticationDao {

    private static Logger logger = LoggerFactory.getLogger("servlets");

    public AuthenticationDao() {
    }

    public User checkUser(String username, String password) {
        User user = null;
        PreparedStatement stmt = null;
        String sql = "select * from user where username = ? and password = ?";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = new DbManager();
        try {
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setString(1, username);
            stmt.setString(2, password);

            rs = stmt.executeQuery();
            System.out.println("Rs size: " + rs.getRow());
            user = new User();
            while (rs.next()) {
                user.setId(rs.getLong("id"));
                user.setName(rs.getString("name"));
                user.setSurname(rs.getString("surname"));
                user.setUsername(rs.getString("username"));
                Role role = new Role();
                role.setId(rs.getLong("role_id"));
                user.setRole(role);
            }

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (user == null) {
            System.out.println("User is null: ");
        }
        return user;
    }

    public int addUser(DbManager dbManager, String name, String surname, String username, String password, Role role) {
        User user = null;
        PreparedStatement stmt = null;
        int effectedRows = 0;

        String sql = "insert into user (name,surname,username,password,role_id) values (?,?,?,?,?)";
        System.out.println("Query: " + sql);
        try {
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setString(1, name);
            stmt.setString(2, surname);
            stmt.setString(3, username);
            stmt.setString(4, password);
            stmt.setLong(5, role.getId());

            effectedRows = stmt.executeUpdate();

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }

        }

        if (user == null) {
            System.out.println("User is null: ");
        }
        return effectedRows;
    }

    public List<User> seeUsers() {
        User user = null;
        PreparedStatement stmt = null;
        List<User> userList = new ArrayList<User>();
        String sql = "select * from user ";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = new DbManager();
        try {
            stmt = dbManager.getConn().prepareStatement(sql);

            rs = stmt.executeQuery();
            System.out.println("Rs size: " + rs.getRow());
            while (rs.next()) {
                user = new User();
                user.setId(rs.getLong("id"));
                user.setName(rs.getString("name"));
                user.setSurname(rs.getString("surname"));
                user.setUsername(rs.getString("username"));
                Role role = new Role();
                role.setId(rs.getLong("role_id"));
                user.setRole(role);
                userList.add(user);
            }
        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (user == null) {
            System.out.println("User is null: ");
        }
        return userList;
    }

    public Role getRoleById(Long id) {
        Role role = null;
        PreparedStatement stmt = null;
        String sql = "select * from role where id = ? ";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = new DbManager();
        try {
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setLong(1, id);

            rs = stmt.executeQuery();
            System.out.println("Rs size: " + rs.getRow());
            role = new Role();
            while (rs.next()) {
                role.setId(rs.getLong("id"));
                role.setName(rs.getString("name"));
            }
        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (role == null) {
            System.out.println("Role is null: ");
        }
        return role;
    }

    public List<Role> seeRoles() {
        Role role = null;
        PreparedStatement stmt = null;
        List<Role> roleList = new ArrayList<Role>();
        String sql = "select * from role ";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = new DbManager();
        try {
            stmt = dbManager.getConn().prepareStatement(sql);

            rs = stmt.executeQuery();
            System.out.println("Rs size: " + rs.getRow());

            while (rs.next()) {
                role = new Role();
                role.setId(rs.getLong("id"));
                role.setName(rs.getString("name"));
                roleList.add(role);
            }
        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (role == null) {
            System.out.println("Role is null: ");
        }
        return roleList;

    }

    public User getUserById(Long id) {
        User user = null;
        PreparedStatement stmt = null;
        String sql = "select * from user where id = ? ";
        System.out.println("Query: " + sql);
        ResultSet rs;
        DbManager dbManager = new DbManager();
        try {
            stmt = dbManager.getConn().prepareStatement(sql);
            stmt.setLong(1, id);

            rs = stmt.executeQuery();
            System.out.println("Rs size: " + rs.getRow());
            user = new User();
            while (rs.next()) {
                user.setId(rs.getLong("id"));
                user.setName(rs.getString("name"));
                user.setSurname(rs.getString("surname"));
                user.setUsername(rs.getString("username"));
                Role role = new Role();
                role.setId(rs.getLong("role_id"));
                user.setRole(role);
            }

        } catch (SQLException ex) {
            System.out.println("Error: " + ex);
            ex.printStackTrace();
            logger.error("Error", ex);
        } finally {
            if (stmt != null)
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            dbManager.close();
        }

        if (user == null) {
            System.out.println("User is null: ");
        }
        return user;
    }

}